<%@page import="edu.fpt.entity.Employee"%>
<%@page import="edu.fpt.entity.BillAll"%>
<%@page import="edu.fpt.stub.BillStub"%>
<%@page import="edu.fpt.entity.Services"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="edu.fpt.entity.RentDetail"%>
<%@page import="edu.fpt.stub.RentDetailStub"%>
<%@page import="edu.fpt.entity.Room"%>
<%@page import="edu.fpt.stub.RoomStub"%>
<%@page import="java.util.List"%>
<%@page import="edu.fpt.entity.Customer"%>
<%@page import="edu.fpt.stub.HotelStub"%>
<%-- 
    Document   : orderForm
    Created on : Jan 20, 2013, 1:08:49 AM
    Author     : TuanAnh
--%>

<%@page import="javax.swing.text.html.HTMLDocument"%>
<%@page import="javax.swing.text.Document"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/modern.css" type="text/css" />       
        <title>Order Form</title>  
        <script type="text/javascript">
            function isNumberKey(evt)
            {
                var charCode = (evt.which) ? evt.which : event.keyCode
                if (charCode > 31 && charCode < 48 || charCode > 57 || charCode == 46)
                    return false;
                return true;
            }
            function isDate(txtDate)
            {
                var currVal = txtDate;
                if(currVal == '')
                    return false;
                //Declare Regex  
                var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; 
                var dtArray = currVal.match(rxDatePattern);
                if (dtArray == null)
                    return false;
                //Checks for mm/dd/yyyy format.
                dtDay = dtArray[3];
                dtMonth = dtArray[5];
                dtYear = dtArray[7];
                if (dtMonth < 1 || dtMonth > 12)
                    return false;
                else if (dtDay < 1 || dtDay> 31)
                    return false;
                else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
                    return false;
                else if (dtMonth == 2)
                {
                    var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
                    if (dtDay> 29 || (dtDay ==29 && !isleap))
                        return false;
                }
                return true;
            }
            $(function() {
                $('#submit').bind('click', function(){
                    var txtVal =  $('#date').val();
                    if(isDate(txtVal))
                        alert('Valid Date! ');
                    else
                        alert('Invalid Date. Pls enter right format.');
                });
            });            
        </script>
    </head>
    <body style="color: darkred; color: " bgcolor="#d9d9d9">    
        <%
            RoomStub roomStub = new RoomStub();
            String dta = request.getParameter("dataRoom");
        %>
        <form name="order" action="orderForm.jsp?dataRoom=<%= dta%>" method="post" >

            <h1 style="color: #4D1124; margin-left: 60px">Please Enter Information to Book Room</h1>
            <hr width="50%" />
            <table>
                <tr>
                    <td style="width:500px">
                        <div style="height: 10px"></div>
                        <h3 style="color: #4D1124; margin-left: 150px;">Enter Your Information</h3>                    
                        <div style="height: 25px"></div>
                        <div class="input-control" style="margin-left: 100px"> Your Name: <input type="text" name="name" placeholder="Enter your name" style="width:230px" required /></div>
                        <div style="height: 6px"></div>
                        <div class="input-control" style="margin-left: 123px"> Gender:  <label class="input-control radio" style="margin-left: 7px"><input type="radio" name="gender" value="true" checked><span class="helper">Male</span></label>
                            <label class="input-control radio"><input type="radio" name="gender" value="false" ><span class="helper">Female</span></label></div>                                        
                        <div class="input-control" style="margin-left: 120px"> Address: <input type="text" name="address" placeholder="Enter your address" style="width:230px" required /></div>
                        <div style="height: 6px"></div>
                        <div class="input-control text" style="margin-left: 71px"> Phone Number: <input type="phone" name="phone" placeholder="Enter your phoneNumber" style="width:230px" onkeypress="return isNumberKey(event)" required /></div>
                        <div style="height: 6px"></div>
                        <div class="input-control text" style="margin-left: 140px"> Email: <input type="email" name="email" placeholder="Enter your email" style="width:230px" required /></div>
                        <div style="height: 6px"></div>
                        <div class="input-control" style="margin-left: 114px"> Passport: <input type="text" name="passport" maxlength="9" placeholder="Please must enter 9 digits." style="width:230px" onkeypress="return isNumberKey(event)" required /></div>
                    </td>
                    <td>   
                        <h3 style="color: #4D1124; margin-left: 120px">Enter Room Information</h3>
                        <div style="height: 30px"></div>
                        <div class="input-control" style="margin-left: 20px">Choose Service:<label class="input-control checkbox" style="margin-left: 7px"><input type="checkbox" name="service" value="S01" /><span class="helper" style="color: #00a300">Spa</span></label>
                            <label class="input-control checkbox" style="margin-left: 2px"><input type="checkbox" name="service" value="S02" /><span class="helper" style="color: #00a300">Rent Car</span></label>                            
                        </div> 
                        <div style="height: 5px"></div>
                        <div class="input-control select" style="width: 230px;margin-left: 28px" >
                            Choose Room:<label style="margin-left: 5px;" > <select class="m-wrap" name="rom">
                                    <%
                                        List<Room> lst = roomStub.getRoomType();
                                        for (Room rom : lst) {
                                            if (rom.getRoomType().getRoomType().equals(dta) && rom.getReservation() != true) {
                                    %>
                                    <option><%= rom.getRoomID()%></option>
                                    <%
                                            }
                                        }
                                    %>
                                </select></label></div>
                        <div style="height: 6px"></div>                       
                        <div class="input-control" style="margin-left: 18px"> Number People: <input type="text" name="number" placeholder="Enter number" style="width:230px" required onkeypress="return isNumberKey(event)" /></div>                    
                        <div style="height: 4px"></div>
                        <div class="input-control" style="margin-left: 50px"> Type Room:</div>  
                        <%
                            try {
                                if (request.getParameter("dataRoom").equals("Standard")) {
                        %>
                        <label class="input-control radio" style="margin-left: 130px"><input type="radio" name="room" value="Standard Room" checked ><span class="helper">Standard Room</span></label>
                        <div style="height: 1px"></div>
                        <% } else if (request.getParameter("dataRoom").equals("Superior")) {
                        %>
                        <label class="input-control radio" style="margin-left: 130px"><input type="radio" name="room" value="Superior Room" checked ><span class="helper">Superior Room</span></label>
                        <div style="height: 1px"></div>
                        <% } else if (request.getParameter("dataRoom").equals("Deluxe")) {
                        %>
                        <label class="input-control radio" style="margin-left: 130px"><input type="radio" name="room" value="Deluxe Room" checked ><span class="helper">Deluxe Room</span></label>
                        <div style="height: 1px"></div>
                        <% } else {%>
                        <label class="input-control radio" style="margin-left: 130px"><input type="radio" name="room" value="Villa" checked ><span class="helper">Villa</span></label>        
                            <% }
                                } catch (Exception ex) {
                                }%>
                        <div style="height: 7px"></div>
                        <div class="input-control" style="margin-left: 49px"> Date Come: <input type="text" name="date" placeholder="Date: [dd/MM/yyyy]" style="width:230px" required /></div>                                                                          
                    </td>
                </tr>
            </table>
            <div style="margin-left: 350px"><input type="submit" value="Submit" name="submit"/><span><input type="reset" value="Reset" style="background-color: #e3a21a; color: white;"/></span><span><input type="button" value="Go Back" style="background-color: #2d89ef; color: white;" onclick="window.location.href='Order.jsp'"/></span></div>            
        </form>
        <div style="height: 15px"></div>
        <%
            if (request.getParameter("submit") != null) {
                try {
                    HotelStub customer = new HotelStub();
                    List<Customer> c = customer.getAll();
                    Customer cus = new Customer();
                    String passport = request.getParameter("passport");
                    String cusID = "C" + passport;
                    RentDetailStub rentStub = new RentDetailStub();
                    RentDetail rent = new RentDetail();

                    rent.setReserDetailId(cusID);
                    String rom = request.getParameter("rom");
                    rent.setRoomId(new Room(rom));
                    String st = request.getParameter("date");
                    rent.setDate(st);
                    rent.setNumber(Integer.parseInt(request.getParameter("number")));
                    String service = request.getParameter("service");
                    if (service != null) {
                        rent.setServiceId(new Services(request.getParameter("service")));
                    } else {
                        rent.setServiceId(new Services("S04"));
                    }
                    rent.setCustomerId(new Customer(cusID));
                    cus.setCustomerId(cusID);
                    cus.setCustomerName(request.getParameter("name"));
                    if (request.getParameter("gender").equals("true")) {
                        cus.setCustomerGender(true);
                    } else {
                        cus.setCustomerGender(false);
                    }
                    cus.setCustomerAddress(request.getParameter("address"));
                    cus.setPhoneNumber(request.getParameter("phone"));
                    cus.setEmail(request.getParameter("email"));
                    cus.setPasspost(passport);
                    if (customer.checkPass(passport) && rom != null) {
                        customer.insertCustomer(cus);
                        rentStub.insertRent(rent);

                        //Room: Check status
                        for (Room r : lst) {
                            if (rent.getRoomId().getRoomID().equals(r.getRoomID())) {
                                r.setReservation(true);
                                if (r.getReceived() == true) {
                                    r.setReceived(false);
                                }
                                roomStub.updateRoom(r);
                            }
                        }

                        //Bill:
                        BillStub billStub = new BillStub();
                        BillAll bi = new BillAll();
                        bi.setBillId(cusID);
                        bi.setDatePay("12/12/2012");
                        bi.setTotal(0);
                        bi.setEmployeeName(new Employee("Tuan Anh"));
                        bi.setReserDetailId(new RentDetail(rent.getReserDetailId()));
                        bi.setDateCome(request.getParameter("date"));
                        bi.setNumberPeople(Integer.parseInt(request.getParameter("number")));

                        billStub.insertBill(bi);

                        out.println("<h3 style='color: red; margin-left: 310px;'>Reservation Success! Welcome to FPT Resort</h3> ");
                    } else {
                        out.println("<h3 style='color: red; margin-left: 310px;'>Reservation Failure! Passport must enter trust value or check format DATE come</h3> ");

                    }
                } catch (Exception ex) {
                    out.println("<h3 style='color: red; margin-left: 310px; font-style: bold'>Reservation Failure! Check DATE your come.</h3>");
                }
            }
        %>

    </body>
</html>
